{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-21T08:20:16.984619Z",
     "start_time": "2025-09-21T08:20:11.824104Z"
    }
   },
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR, 'div')\n",
    "for element in elements:\n",
    "    print(element.text)\n"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n",
      "狮子\n",
      "老虎\n",
      "山羊\n",
      "张三\n",
      "内层11\n",
      "内层12\n",
      "内层21\n",
      "内层11\n",
      "内层12\n",
      "内层11\n",
      "内层12\n",
      "内层21\n",
      "内层21\n",
      "版权 发布日期：2018-03-03\n",
      "备案号 苏ICP备88885574号\n",
      "版权 发布日期：2018-03-03\n",
      "备案号 苏ICP备88885574号\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### # id选择器",
   "id": "f630446886c4ee82"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T08:21:42.236677Z",
     "start_time": "2025-09-21T08:21:38.019202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "# id选择\n",
    "element = wd.find_element(By.CSS_SELECTOR, '#searchtext')\n",
    "element.send_keys('你好')"
   ],
   "id": "2b3fe4262e4d2054",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### [] 属性选择器\n",
    " - *= 包含\n",
    " - ~= 单词匹配\n",
    " - ^= 前缀匹配\n",
    " - $= 后缀匹配"
   ],
   "id": "87a1775d0ac590c9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T08:33:00.338718Z",
     "start_time": "2025-09-21T08:32:54.217172Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "# 属性选择\n",
    "element = wd.find_element(By.CSS_SELECTOR, 'a[href*=\"miitbeian\"]')\n",
    "print(element.get_attribute('outerHTML'))"
   ],
   "id": "df0d3098f021441c",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a href=\"http://www.miitbeian.gov.cn\">苏ICP备88885574号</a>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### . 类选择",
   "id": "42f1bb8833385ad1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T09:55:58.925225Z",
     "start_time": "2025-09-21T09:55:51.729003Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "# 类选择/组选择\n",
    "elements = wd.find_elements(By.CSS_SELECTOR, '.plant, .animal')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "id": "e9ffcf4d771b91a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n",
      "狮子\n",
      "老虎\n",
      "山羊\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 标签选择",
   "id": "99ff618bb12634e9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T08:00:29.029787Z",
     "start_time": "2025-09-22T08:00:23.366133Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "# 标签选择\n",
    "elements = wd.find_elements(By.CSS_SELECTOR, 'a')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "id": "f0b9259be8734c20",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<selenium.webdriver.remote.webelement.WebElement (session=\"630dc8d42424f0370899d1f6bd0e19d2\", element=\"f.167DF5AF1BB16DC659C9FA7CD64ABB63.d.87B756C08AAFBBD1603BDCB8E061A78D.e.3\")>\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 组选择",
   "id": "e78085ebc1752668"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T08:09:24.110714Z",
     "start_time": "2025-09-22T08:09:20.052707Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1a.html')\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR, '#t1>span, #t1>p')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)"
   ],
   "id": "14db8ac67ba2c8e3",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李白\n",
      "静夜思\n",
      "杜甫\n",
      "春夜喜雨\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 按次序选择子节点",
   "id": "8281799872c53f49"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-22T08:20:08.450249Z",
     "start_time": "2025-09-22T08:20:02.567548Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1b.html')\n",
    "\n",
    "# 按次序选择子节点\n",
    "elements_1 = wd.find_elements(By.CSS_SELECTOR, 'div>span:nth-child(2)')\n",
    "\n",
    "# 父元素的倒数第n个子节点\n",
    "elements_2 = wd.find_elements(By.CSS_SELECTOR, 'div>span:nth-last-child(2)')\n",
    "\n",
    "# 父元素的第几个某类型的子节点\n",
    "elements_3 = wd.find_elements(By.CSS_SELECTOR, 'div>span:nth-of-type(1)')\n",
    "\n",
    "# 父元素的倒数第几个某类型的子节点\n",
    "elements_4 = wd.find_elements(By.CSS_SELECTOR, 'div>span:nth-last-of-type(1)')\n",
    "\n",
    "# 奇数(odd)节点和偶数(even)节点\n",
    "elements_5 = wd.find_elements(By.CSS_SELECTOR, 'div>span:nth-of-type(odd)')\n",
    "\n",
    "# 相邻兄弟节点选择\n",
    "elements_6 = wd.find_elements(By.CSS_SELECTOR, 'div>h3+span')\n",
    "\n",
    "for element in elements_6:\n",
    "    print(element.text)"
   ],
   "id": "a256010f1d7e9e86",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李白\n",
      "苏轼\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "2f3c5f262d9ebe5c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
